package com.shigu.item.service;

import com.gexin.fastjson.JSON;
import com.opentae.data.mall.interfaces.MallItemMapper;
import com.shigu.exceptions.AdminException;
import com.shigu.item.vo.*;
import com.shigu.main4.appmall.bo.GetItemDaifaInfoBO;
import com.shigu.main4.appmall.bo.GetShiguGoodsBO;
import com.shigu.main4.appmall.service.AppItemGoodsService;
import com.shigu.main4.appmall.vo.GetItemDaifaInfoVO;
import com.shigu.main4.appmall.vo.GetShiguGoodsVO;
import com.shigu.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * create by  过儿  2019-07-08
 * 星座的服务
 */
@Service
public class XzDaifaItemService {


    @Autowired
    MallItemMapper mallItemMapper;
    @Autowired
    AppItemGoodsService appItemGoodsService;

    /**
     *
     *
     * @description 获取shigu商品
     * @author 郭尔
     * @date 2019-07-08 16:57
     */
    public ShiguGoodsResponse getShiguGoods(String itemCode, String webSite) throws AdminException {
        GetShiguGoodsBO bo = new GetShiguGoodsBO();
        bo.setItemCode(itemCode);
        bo.setWebSite(webSite);
        GetShiguGoodsVO dto = appItemGoodsService.findGoodsByIdAndWebSite(bo);

        if (dto ==null){
            throw new AdminException("找不到该商品信息,请确认商品编号是否正确");
        }

        ShiguGoodsResponse response = new ShiguGoodsResponse();

        response.setGoodsId(dto.getGoodsId());
        response.setSkus(JSON.toJSONString(dto.getSkus()));
        response.setStoreNum(dto.getStoreNum());
        response.setProps(JSON.toJSONString(dto.getProp()));
        response.setGoodsNo(dto.getGoodsNo());
        response.setTitle(dto.getTitle());
        response.setPiPrice(dto.getPiPrice());
        response.setDetailUrl(StringUtils.join(dto.getDetailUrls(), ","));
        response.setPicUrl(StringUtils.join(dto.getImgUrl(), ","));
        //计算价格
        //计算公式 价格 / 0.65 + 10
        Double base = MoneyUtil.divide(response.getPiPrice(), "0.65");
        Double appPrice = MoneyUtil.add(Double.toString(base), "10");
        response.setAppPrice(MoneyUtil.formatPriceMoney(appPrice));
        return response.success();
    }

    /**
     * @description 获取服务商
     * @author 郭尔
     * @date 2019-07-08 17:33
     */
    public ItemDaifaResponse getItemDaifaInfo(String itemCode, String webSite) throws AdminException {
        GetItemDaifaInfoBO bo = new GetItemDaifaInfoBO();
        bo.setItemCode(itemCode);
        bo.setWebSite(webSite);
        GetItemDaifaInfoVO dto = appItemGoodsService.findItemDaifaInfo(bo);
        if(dto ==null ){
            throw new AdminException("未能获取到相关信息，请确认商品编号是否正确");
        }
        ItemDaifaResponse response = new ItemDaifaResponse();
        BeanUtils.copyProperties(dto,response);
        return response.success();
    }
}
